import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_function.dart';
import 'package:enterprise_v6_app/app/core/constans/dict_data_info.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/common/choice_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/emergency/emergency_res_team_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/get_dict_all_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/emergency/resource/model.dart';

/// 应急队伍
class EmergencyResourceTeamController extends GetxController {
  final popRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 搜索使用key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 筛选列表
  List<FilterBean> filterList = [];

  /// 队伍类型
  List<SimpleChoiceBean> teamType = [];

  /// 所选择的队伍类型
  SimpleChoiceBean? teamT;
  String? teamName;

  /// 评估状态
  EmergencyAssessStatus? assess;

  /// 单位
  String? affiliatedUnit;

  @override
  void onInit() {
    super.onInit();
    initData();
  }

  /// 初始化数据
  Future<void> initData() async {
    final List<GetDictAllEntity> dictTypeList = await DictDataInfo.getDictionaryDataByTree(dictionaryType: 'yjdwfl_Eo5T07uU');
    teamType.addAll(dictTypeList.map((e) => SimpleChoiceBean(e.dictionaryLabel!, e.id!)));
  }

  Future<List<EmergencyResTeamEntity>> getListData(int pageNum, int pageSize) async {
    final res = await HttpUtil().generateGetRequest<List<EmergencyResTeamEntity>>(
      '${Interface.mainBaseUrl}/emergency//emergencyTeam/getEmergencyTeamPage',
      queryParameters: {
        'pageNum': pageNum,
        'pageSize': pageSize,
        'teamName': teamName,
        'assess': assess,
        'affiliatedUnit': affiliatedUnit,
      },
    ).execute();
    return res.data ?? [];
  }

  /// 筛选
  void screen() {
    popRightScaffoldKey.currentState?.openEndDrawer();
  }

  /// 搜索
  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }

  /// 显示选择评估状态
  void showSelect() {
    CustomSelectFunction.selectMultipleBottomSheet<EmergencyAssessStatus>(
      onCallBack: (val) {
        assess = val.first;
        update();
      },
      dataList: EmergencyAssessStatus.assessList,
      checkedList: assess == null ? [] : [assess!],
      isMultiple: false,
    );
  }

  /// 显示选择单位
  void showSelectTeamType() {
    CustomSelectFunction.selectMultipleBottomSheet<SimpleChoiceBean>(
      onCallBack: (val) {
        teamT = val.first;
        update();
      },
      dataList: teamType,
      checkedList: teamT == null ? [] : [teamT!],
      isMultiple: false,
    );
  }

  void clearChecked() {
    assess = teamName = teamT = affiliatedUnit = null;
    update();
  }

  void confirm() {
    popRightScaffoldKey.currentState?.closeEndDrawer();
    refreshPage();
  }
}
